#coding=utf-8
from selenium import webdriver
import unittest
import sys
import pytest
import os
import time
from common.config import *


# username='admin'
# password='admin'
class TestLogin(object):
	@classmethod
	def setup_method(self):
		self.driver = webdriver.Chrome()
		self.driver.maximize_window()
		#url = "http://dayuv4.rongyi.com/RYPlatform/user/login"
		url = "http://fe1.rongyi.com:8186/RYPlatform/user/login"
		self.driver.get(url)
		title = self.driver.title
		print (title)
		# now_url = self.driver.current_url
		# print (now_url)
		username = "admin"
		password = "admin"
		# 执行登录操作
		self.driver.find_element_by_id("account").clear()
		self.driver.find_element_by_id("account").send_keys(username)
		self.driver.find_element_by_id("password").clear()
		self.driver.find_element_by_id("password").send_keys(password)
		# 点击登录
		self.driver.find_element_by_xpath("/html/body/div/div/div[2]/form/div[3]/div/button").click()

	# @classmethod
	# def teardown_method(self):
	# 	self.driver.quit()

	def test_newgame(self):
		# 登录成功
		# 切换到游戏平台,点击游戏管理
		self.driver.implicitly_wait(5)
		#self.driver.find_element_by_xpath("//*[@id='195']").click()
		#self.driver.find_element_by_xpath("//*[@id='201']/div/span/a").click()
		self.driver.find_element_by_xpath("//*[@id='246']").click()
		self.driver.find_element_by_xpath("//*[@id='259']/div/span/a").click()
		self.driver.implicitly_wait(5)
		# 点击新增游戏
		iframe=self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div[2]/div/div[2]/div/iframe")
		self.driver.switch_to.frame(iframe)
		self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div/div/div/div/div/div[2]/div/div/div/div/form/div/div[5]/button").click()
		windowAdd = self.driver.window_handles[1]
		self.driver.switch_to_window(windowAdd)
		#self.driver.switch_to.window(self.driver.window_handles[1])
		self.driver.implicitly_wait(10)
		iframe1=self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div[2]/div/div[2]/div/iframe")
		self.driver.switch_to.frame(iframe1)
		# 基本信息
		gamename='game'+str(round(time.time() * 1000))
		self.driver.find_element_by_xpath("//*[@id='name']").send_keys(gamename)
		self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div/div/div/div/div/div[2]/div/div/div[2]/form[1]/div/div[2]/div[2]/span/span[1]/span[1]").click()
		self.driver.find_element_by_xpath("/html/body/div[2]/ul/li[2]/div/span").click()
		self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div/div/div/div/div/div[2]/div/div/div[2]/form[1]/div/div[3]/div[2]/span/span[1]/span[1]").click()
		self.driver.find_element_by_xpath("/html/body/div[3]/ul/li[2]/div").click()
		self.driver.find_element_by_xpath("//*[@id='h5Url']").send_keys(geturl())
		self.driver.find_element_by_xpath("//*[@id='preview']").send_keys(img_path+'1080xiao.png')
		time.sleep(2)
		self.driver.find_element_by_xpath("//*[@id='richText']/label[1]/span[2]").click()
		self.driver.find_element_by_xpath("//*[@id='privacyPolicy']/label[1]").click()
		self.driver.find_element_by_xpath("//*[@id='playType']/label[1]/span[1]").click()
		# 美术资源管理
		self.driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div/div/div/div[2]/div/div/div[4]/form/div/div[1]/div[2]/span/input").send_keys('皮肤包'+str(round(time.time() * 1000)))
		self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div/div/div/div/div/div[2]/div/div/div[4]/form/div/div[2]/div[2]/div/div[1]/input").send_keys(img_path+'pifubao.zip')
		self.driver.find_element_by_xpath("//*[@id='previewSkin']").send_keys(img_path+'1080xiao.png')
		# 互动营销玩法配置
		target = self.driver.find_element_by_xpath("//*[@id='trick']/label[1]/span[1]/span")
		self.driver.execute_script("arguments[0].scrollIntoView();", target)
		self.driver.find_element_by_xpath("//*[@id='trick']/label[1]/span[2]").click()
		self.driver.find_element_by_xpath("//*[@id='stageForm']/label[1]/span[2]").click()
		self.driver.find_element_by_xpath("//*[@id='sessionType']/label[1]/span[2]").click()
		self.driver.find_element_by_xpath("//*[@id='duration']/label[1]/span[2]").click()
		self.driver.find_element_by_xpath("//*[@id='timesPerRound']/label[1]/span[2]").click()
		self.driver.find_element_by_xpath("//*[@id='gameDifficulty']/label[1]/span[2]").click()
		# 门槛、裂变和发奖机制
		# 参与门槛
		self.driver.find_element_by_id("threshold").click()
		self.driver.find_element_by_xpath("/html/body/div[4]/div/div/ul/li[1]/div/label/span/input").click()
		self.driver.find_element_by_xpath("/html/body/div[4]/div/div/ul/li[2]/div/label/span/input").click()
		# 白名单数据来源
		self.driver.find_element_by_xpath("//*[@id='whiteSources']/label[1]/span[1]/input").click()
		self.driver.find_element_by_xpath("//*[@id='whiteSources']/label[2]/span[1]/input").click()
		# 参与次数增加方式
		self.driver.find_element_by_xpath("//*[@id='addPartTimes']/label[1]/span[1]/input").click()
		self.driver.find_element_by_xpath("//*[@id='addPartTimes']/label[2]/span[1]/input").click()
		self.driver.find_element_by_xpath("//*[@id='addPartTimes']/label[3]/span[1]/input").click()
		self.driver.find_element_by_xpath("//*[@id='addPartTimes']/label[4]/span[1]/input").click()
		# 发奖条件
		self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div/div/div/div/div/div[2]/div/div/div[8]/form/div/div[1]/div[2]/span/span[1]/span[2]/span/i").click()
		self.driver.find_element_by_xpath("//*[@id='awardManner']/label[1]/span[1]").click()
		# self.driver.find_element_by_xpath("//*[@id='awardManner']/label[2]/span[1]").click()
		# 微信授权方式
		self.driver.find_element_by_xpath("//*[@id='authorizeTypes']/label[1]/span[1]").click()
		self.driver.find_element_by_xpath("//*[@id='authorizeTypes']/label[2]/span[1]").click()
		# 提交
		time.sleep(4)
		# self.driver.find_element_by_xpath("//*[@id='ice-container']/div/div/div/div/div/div/div[2]/div/div/div[11]/div/div/div/button[1]").click()
		# time.sleep(4)
		# self.driver.close()
		return gamename

	def test_shouquan(self):
		# gamename=self.test_newgame()
		gamename="game1595571462681"
		# 授权
		self.driver.switch_to.window(self.driver.window_handles[0])
		self.driver.implicitly_wait(5)
		self.driver.find_element_by_xpath("//*[@id='256']").click()
		self.driver.refresh()
		self.driver.implicitly_wait(5)
		iframe2=self.driver.find_element_by_xpath("//*[@id='ice-container']/div/iframe")
		self.driver.switch_to.frame(iframe2)
		self.driver.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/div[1]/div[4]/div/div[1]/div/input").send_keys(gamename)
		self.driver.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/div[1]/div[5]/button/span").click()
		self.driver.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/div[2]/div[2]/div[2]/div/div/div/table/tbody/tr/td[9]/div/button[4]/span").click()
		self.driver.find_element_by_xpath("//*[@id='app']/div/div[2]/div[2]/div/form/div[2]/div[2]/div/div[2]/button").click()


if __name__ == "__main__":
    command='pytest -s --env=test webtest.py::TestLogin::test_newgame'
    os.system(command)
	# TestLogin.test_newgame("http://fe1.rongyi.com:8186/RYPlatform/user/login","admin","admin")
